home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / e / amigae33a.lha / E_v3.3a / Src.lha / Src / Afc / Tasker_Example2.e < prev    next >
Text File  |  1997-08-02  |  3KB  |  95 lines

  1.  
  2. MODULE 'AFC/tasker', 'AFC/explain_exception',
  3.        'exec/ports','exec/memory','exec/nodes'
  4.  
  5.  
  6. -> a complex message
  7. OBJECT complex
  8.   mnode:mn  ->BE CAREFUL: This IS the structure, NOT the pointer
  9.   wow:LONG
  10. ENDOBJECT
  11.  
  12. -> the tasker structure IS DEFined global because we use it also in the
  13. -> process' body.
  14. DEF myp:PTR TO tasker
  15.  
  16. PROC thread()
  17.   DEF message:PTR TO complex
  18.  
  19. -> get the global data pointer, previously stored by the main process.
  20. -> IMPORTANT: DO this before using global variables OR functioncalls.
  21.   geta4()
  22.  
  23. /*
  24.   IF you DEF'd your tasker ('myp' in this CASE) as a global variable,
  25.   you can access its port by the dosport() method. IF you need TO make it a
  26.   local variable THEN, TO get the port associated TO the process, you
  27.   follow the standard way:
  28.  
  29.     DEF thisthread:PTR TO process
  30.     thisthread:=FindTask(0)
  31.     dosport:=thisthread.msgport
  32.  
  33.   Warning: the use OF this port IS reserved TO AmigaDOS, so create your
  34.            own port IF you need one.
  35. */
  36.  
  37.   -> create a port associated TO the process (this method must be called
  38.   -> from the task that needs the port, not by the main program).
  39.   myp.buildport()
  40.  
  41.   WaitPort(myp.port())
  42.   message:=GetMsg(myp.port())
  43.  
  44.   PrintF('Hello, I''m a NEW thread. Wow IS:\d\n', message.wow)
  45.   Delay(50)
  46.  
  47. -> make sure there IS no taskswitching after we replied the message
  48.   Forbid()
  49.   ReplyMsg(message)
  50.   myp.endport()  -> remember TO close the port before ending.
  51. ENDPROC
  52.  
  53. PROC main() HANDLE
  54.   DEF mainp=NIL, mess=NIL:PTR TO complex
  55.  
  56. -> Store the global data pointer: DO this BEFORE any process IS started.
  57. -> Remember TO geta4() in the process before using any global variable OR
  58. -> functioncall!
  59.   storea4()
  60.  
  61.   NEW myp.tasker('simpleprocess')  -> name AND initialization
  62.   myp.code({thread})   -> the code TO be used
  63.  
  64.   IF (mainp:=buildPort())  -> we need a port in the main() process TO
  65.                            -> communicate with thread()
  66.  
  67.     WriteF('Starting the process...\n')
  68.     myp.process()  -> starting the process
  69.  
  70.     mess:=NewM(SIZEOF complex, MEMF_PUBLIC OR MEMF_CLEAR) -> alloc AND
  71.     setupMsg(mess, SIZEOF complex, mainp)             -> setup the message
  72.     mess.wow:=10    -> fill in your additional fields
  73.  
  74.     WriteF('Sending the message...\n')
  75.     REPEAT              -> wait FOR the process TO create its own port
  76.       Delay(5)          -> before sending anything TO it.
  77.     UNTIL myp.port()
  78.  
  79.     myp.send(mess)  -> send the message TO thread()
  80.  
  81.     WaitPort(mainp)  -> wait FOR an answer.
  82. -> IF process doesn't reply we can't Dispose the message memory 'cause
  83. -> the thread may be still using it.
  84.   ELSE
  85.     WriteF('well, no port!\n')
  86.   ENDIF
  87.  
  88. EXCEPT DO
  89.   IF mess THEN Dispose(mess)  -> remember TO free the memory
  90.   IF mainp THEN endPort(mainp)
  91.   END myp
  92.   explain_exception()
  93. ENDPROC
  94.  
  95.